#include "loading.h"

Loading::Loading(QWidget *parent) : QWidget(parent)
{
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [&]{
        imageRotate += 10;
        if(imageRotate >= 360)
            imageRotate = 0;
        update();
    });
}

void Loading::startLoading(QString icon, int interval)
{
    this->icon = icon;
    timer->start(interval);
}

void Loading::stop(bool clear)
{
    timer->stop();
    if(clear)
        icon.clear();
    update();
}

void Loading::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap disc(icon);

    qreal x = rect().x() + (rect().width() / 2);
    qreal y = rect().y() + (rect().height() / 2);

    painter.save();
    /* 设定旋转中心点 */
    painter.translate(x, y);
    /* 旋转的角度 */
    painter.rotate(imageRotate);
    /* 画图操作 */
    painter.drawPixmap(-rect().width()/2, -rect().width()/2, rect().width(), rect().height(), disc);
    painter.restore();
}
